<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        img {
            display: none;
            transition: all 2s;
            width: 600px;
            height: 230px;
            position: absolute;
            left: 0;
            top: 0;

        }

        .main {
            position: relative;
            width: 600px;
            height: 230px;
        }

        .dots {
            position: absolute;
            bottom: 15px;
            right: 240px;
            line-height: 12px;

        }

        .dots span {
            display: inline-block;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            margin-left: 8px;
            background-color: rgba(7, 17, 27, 0.4);
            cursor: pointer;
            box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.8) inset;
        }

        .button {
            position: absolute;
            transform: rotate(180deg);
            top: 50%;
            left: 0;
            height: 80px;
            width: 40px;
            margin-top: -40px;
            background: url(./img/arrow.png) center center no-repeat;
        }

        .next {
            transform: rotate(0deg);
            left: auto;
            right: 0;
        }
    </style>

</head>

<body>
    <div class="main" id="main">
        <img src="./img/bg1.jpg" style="display: inline-block;" alt="">
        <img src="./img/bg2.jpg" alt="">
        <img src="./img/bg3.jpg" alt="">

        <a href="javascript:void(0)" class="button prev" id="prev"></a>
        <a href="javascript:void(0)" class="button next" id="next"></a>
        <div class="dots" id="dots">
            <span class="active"></span>
            <span></span>
            <span></span>
        </div>

    </div>



    <script>
        var imgs = document.getElementsByTagName("img");
        var prevBtn = document.getElementById("prev");
        var nextBtn = document.getElementById("next");
        var dots = document.getElementById("dots").getElementsByTagName("span");

        var mainDiv = document.getElementById("main");


        mainDiv.onmouseover = StopAutoPlay;

        mainDiv.onmouseout = StartAutoPlay;

        for (let i = 0; i < dots.length; i++) {
            dots[i].onclick = function () {
                HideLastImg();
                curIndex = i;
                ShowCurrImg();
            }
        }

        prevBtn.onclick = function () {
            HideLastImg();
            curIndex--;
            if (curIndex < 0) {
                curIndex = imgs.length - 1;
            }
            ShowCurrImg();
        };

        nextBtn.onclick = function () {

            HideLastImg();
            curIndex++;//2+1
            if (curIndex == imgs.length) {
                curIndex = 0;
            }
            ShowCurrImg();
        };

        let curIndex = 0;
        let timer = null;
        function StartAutoPlay() {
            timer = setInterval(function () {

                HideLastImg();
                curIndex++;

                if (curIndex >= imgs.length) {
                    curIndex = 0;
                }
                ShowCurrImg();
            }, 2000);
        }

        function StopAutoPlay() {
            clearInterval(timer)
        }

        function HideLastImg() {
            // for (let i = 0; i < imgs.length; i++) {
            //     imgs[i].style.display = "none";
            // }

            imgs[curIndex].style.display = "none";
        }

        function ShowCurrImg() {
            // HideLastImg();

            imgs[curIndex].style.display = "inline-block";
        }


        StartAutoPlay();
    </script>
</body>

</html>